﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tunnel.DevicesControls.Lamp
{
    public partial class Light_BasicLED22Form : LampForm
    {
        Light_BasicLED22 Lamp = new Light_BasicLED22();
        public Light_BasicLED22Form(Light_BasicLED22 Lamp)
        {
            InitializeComponent();
            this.Lamp = Lamp;
            switch (this.Lamp.StatusDescription)
            {
                case "点亮": powerOn.Checked = true; break;
                case "熄灭": powerOff.Checked = true; break;
            }
            this.lampName.Text = "基本照明2.2";
            if (!Global.PlcStatus[Lamp.Protocol])
            {
                ok.Enabled = false;
            }
        }

        private void ok_Click(object sender, EventArgs e)
        {
            if (powerOn.Checked == true)
            {
                //Lamp.StatusDescription = "点亮";
                bwPowerOnCommand.RunWorkerAsync();
                this.Close();
                return;
            }
            if (powerOff.Checked == true)
            {
                //Lamp.StatusDescription = "熄灭";
                bwPowerOffCommand.RunWorkerAsync();
                this.Close();
                return;
            }
            MessageBox.Show("请选择照明状态");
        }

        private void exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bwPowerOnCommand_DoWork(object sender, DoWorkEventArgs e)
        {
            Global g = new Tunnel.Global();
            string CommandID = g.CommandID;
            string sql = "INSERT INTO R_Command(CommandID,DeviceID,ParamName,ParamValue,UpdateTime,UpdateBy,Runned) VALUES('" + CommandID + "','" + Lamp.DeviceID + "','" + Lamp.ParamID + "','1',GETDATE(),'" + Global.User + "',0)";

            SqlDBHelper sdh = new SqlDBHelper();

            sdh.ExecuteCommand(sql);
        }

        private void bwPowerOffCommand_DoWork(object sender, DoWorkEventArgs e)
        {
            Global g = new Tunnel.Global();
            string CommandID = g.CommandID;
            string sql = "INSERT INTO R_Command(CommandID,DeviceID,ParamName,ParamValue,UpdateTime,UpdateBy,Runned) VALUES('" + CommandID + "','" + Lamp.DeviceID + "','" + Lamp.ParamID + "','0',GETDATE(),'" + Global.User + "',0)";

            SqlDBHelper sdh = new SqlDBHelper();

            sdh.ExecuteCommand(sql);
        }
    }
}
